package com.mlnx.yingbaisui.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mlnx.yingbaisui.sys.service.YbsUserService;
import org.herui.campus.common.enums.ResponseEnum;
import org.herui.campus.common.exception.ServiceException;
import org.herui.campus.sys.entity.User;
import org.herui.campus.sys.enums.UserIdentityEnum;
import org.herui.campus.sys.iml.SysBaseService;
import org.herui.campus.sys.mysql.UserMapper;
import org.herui.campus.sys.service.UserService;
import org.shan.base.layer.user.entity.Role;
import org.shan.base.layer.user.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * author name: amanda.shan
 * create time: 2020-05-19 13:13:54
 */
@Service
public class YbsUserServiceImpl extends SysBaseService<UserMapper, User> implements YbsUserService {
    @Autowired
    UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    PasswordEncoder passwordEncoder;

    @Override
    @Transactional
    public void register(Long uuid, String name) {
        Role role = roleService.getOne(new QueryWrapper<Role>().eq("role_key", "user"));
        if (role == null) {
            throw new ServiceException(ResponseEnum.ROLE_FAIL.getCode(), "角色不存在");
        }

        String encode = passwordEncoder.encode("123456");
        int id = saveAccount(uuid.toString(), encode, role.getId(), UserIdentityEnum.USER_STAFF.toString());
        User user = new User();
        user.setId(id);
        user.setCountry("中国");
        userService.save(wrapperCreater(user));
    }
}

